package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.TemplateListDto;
import com.aiti.lulian.dto.newCard.CardMemberInfoDto;
import com.aiti.lulian.dto.newCard.EnterpriseCardTemplateComponentDto;
import com.aiti.lulian.dto.newCard.EnterpriseCardTemplateNewSaveDto;
import com.aiti.lulian.entity.newCard.CardComponentData;
import com.aiti.lulian.service.newCard.CardEnterpriseTemplateNewService;
import com.aiti.lulian.vo.PreSetTemplateListVo;
import com.aiti.lulian.vo.TemplateListVo;
import com.aiti.lulian.vo.newCard.EnterpriseMediaInfoVo;
import com.aiti.lulian.vo.newCard.TemplateComponentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-08-25 17:08
 **/
@RestController
@RequestMapping("/new-card")
@Slf4j
@Api(tags = "企业名片模板接口")
public class CardEnterpriseTemplateNewController extends WebController {

    @Autowired
    private CardEnterpriseTemplateNewService cardEnterpriseTemplateNewService;

    @GetMapping("/query-template-info")
    @ApiOperation("查询模板信息")
    public Message queryTemplateInfo(@RequestParam("templateId") String templateId) {
        try {
            TemplateComponentVo templateComponentVo = cardEnterpriseTemplateNewService.queryTemplateInfo(templateId);
            return Message.success(templateComponentVo);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询模板信息异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/save-or-update-template-info")
    @ApiOperation("保存或更新模板信息")
    public Message saveOrUpdateTemplateInfo(@RequestBody EnterpriseCardTemplateComponentDto dto) {
        try {
            cardEnterpriseTemplateNewService.saveOrUpdateTemplateInfo(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("保存或更新模板信息异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-enterprise-media-info")
    @ApiOperation("查询企业媒体信息")
    public Message getEnterpriseMediaInfo(@RequestParam("enterpriseId") String enterpriseId) {
        try {
            EnterpriseMediaInfoVo enterpriseMediaInfoVo = cardEnterpriseTemplateNewService.getEnterpriseMediaInfo(enterpriseId);
            return Message.success(enterpriseMediaInfoVo);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业媒体信息异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/get-enterprise-product-list")
    @ApiOperation("查询企业产品列表")
    public Message getEnterpriseProductList(@RequestParam("enterpriseId") String enterpriseId) {
        try {
            return Message.success(cardEnterpriseTemplateNewService.getEnterpriseProductList(enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业产品列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/get-enterprise-service-list")
    @ApiOperation("查询企业服务列表")
    public Message getEnterpriseServiceList(@RequestParam("enterpriseId") String enterpriseId) {
        try {
            return Message.success(cardEnterpriseTemplateNewService.getEnterpriseServiceList(enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业服务列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/get-enterprise-material-list")
    @ApiOperation("查询企业素材列表")
    public Message getEnterpriseMaterialList(@RequestParam("enterpriseId") String enterpriseId) {
        try {
            return Message.success(cardEnterpriseTemplateNewService.getEnterpriseMaterialList(enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业素材列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/get-enterprise-material-detail")
    @ApiOperation("查询企业素材详情")
    public Message getEnterpriseMaterialDetail(@RequestParam("materialId") String materialId) {
        try {
            return Message.success(cardEnterpriseTemplateNewService.getEnterpriseMaterialDetail(materialId));
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业素材详情异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/preSetTemplateList")
    @ApiOperation("查询预置模板列表")
    public Message preSetTemplateList(TemplateListDto dto) {
        List<PreSetTemplateListVo> preSetTemplateListVos = cardEnterpriseTemplateNewService.preSetTemplateList(dto);
        return Message.success(preSetTemplateListVos);

    }

    @GetMapping("/standardTemplateList")
    @ApiOperation("查询预置模板列表")
    public Message standardTemplateList(TemplateListDto dto) {
        List<PreSetTemplateListVo> preSetTemplateListVos = cardEnterpriseTemplateNewService.standardTemplateList(dto);
        return Message.success(preSetTemplateListVos);

    }

    @GetMapping("/preSetTemplateDetailInfo")
    public Message preSetTemplateDetailInfo(@RequestParam String baseId){
        PreSetTemplateListVo detailInfo = cardEnterpriseTemplateNewService.preSetTemplateDetailInfo(baseId);
        return Message.success(detailInfo);

    }


    @GetMapping("/templateList")
    @ApiOperation("查询模板列表")
    public Message templateList(TemplateListDto dto) {
        List<TemplateListVo> templateListVos = cardEnterpriseTemplateNewService.templateList(dto);
        return Message.success(templateListVos);

    }

    @PostMapping("/save-or-update-pre-template-info")
    @ApiOperation("保存或更新预置模板信息-运营平台使用")
    public Message saveOrUpdatePreTemplateInfo(@RequestBody EnterpriseCardTemplateComponentDto dto) {
        try {
            if(StringUtils.isEmpty(dto.getUserId())){
                dto.setUserId(findUserId());
            }
            cardEnterpriseTemplateNewService.saveOrUpdatePreTemplateInfo(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("保存或更新预置模板信息异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/templateVersion")
    @ApiOperation("获取当前名片的模板版本")
    public Message templateVersion(@RequestParam("cardId") String cardId){
        return cardEnterpriseTemplateNewService.queryTemplateVersionByCardId(cardId);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除模板")
    public Message delete(@RequestParam String baseId) {
        return cardEnterpriseTemplateNewService.deleteTemplate(baseId);
    }


    @PostMapping("/edit-template-card-info")
    @ApiOperation("企业名片编辑模板信息")
    public Message editTemplateCardInfo(@RequestBody EnterpriseCardTemplateNewSaveDto dto) {
        try {
            cardEnterpriseTemplateNewService.editTemplateCardInfo(dto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("保存或更新模板信息异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }


    @GetMapping("/templateDetailInfo")
    @ApiOperation("企业名片小程序查看模板详情信息")
    public Message templateDetailInfo(@RequestParam String baseId) {
        return Message.success(cardEnterpriseTemplateNewService.templateDetailInfo(baseId));
    }

    @DeleteMapping("/adminDeleteTemplate")
    @ApiOperation("企业名片小程序企业管理员删除模板")
    public Message adminDeleteTemplate(@RequestParam String baseId){
        return cardEnterpriseTemplateNewService.adminDeleteTemplate(baseId);
    }

    @GetMapping("/get-member-components-info")
    @ApiOperation("获取成员组件信息")
    public Message getMemberComponentsInfo(@RequestParam String cardId) {
        return Message.success(cardEnterpriseTemplateNewService.getMemberComponentsInfo(cardId));
    }

    @PutMapping("/edit-member-components-data")
    @ApiOperation("编辑成员组件数据")
    public Message editMemberComponentsData(@RequestBody CardComponentData cardComponentData) {
        cardEnterpriseTemplateNewService.editMemberComponentsData(cardComponentData);
        return Message.success();
    }

    /**
     * 名片模板数据 + 组件 一起保存
     */
    @PutMapping("/edit-member-info")
    @ApiOperation("编辑成员资料")
    public Message editMemberInfo(@RequestBody CardMemberInfoDto dto) {
        return cardEnterpriseTemplateNewService.editMemberInfo(dto);
    }

    @GetMapping("/get-member-template-info")
    @ApiOperation("获取成员模板数据")
    public Message getMemberTemplateInfo(@RequestParam(value = "styleId") String styleId, @RequestParam(value = "templateId", required = false) String templateId,
                                         @RequestParam(value = "cardId") String cardId, @RequestParam(value = "templateType") String templateType) {
        return cardEnterpriseTemplateNewService.getMemberTemplateInfo(styleId, templateId, cardId, templateType);
    }

    @DeleteMapping("/delete-enterprise-template")
    @ApiOperation("删除企业模板")
    public Message deleteEnterpriseTemplate(@RequestParam("templateId") String templateId, @RequestParam("userId")String userId) {
        try{
            if(StringUtils.isEmpty(userId)){
                userId = findUserId();
                if(StrUtil.isBlank(userId)) {
                    return Message.fail("请先登录");
                }
            }
            cardEnterpriseTemplateNewService.deleteEnterpriseTemplate(templateId,userId);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("删除企业模板异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("删除异常");
        }
    }

    @PostMapping("/choose_save-template-info")
    @ApiOperation("用户登录企业后台选择并保存企业及模板信息")
    public Message chooseSaveTemplateInfo(@RequestBody EnterpriseCardTemplateComponentDto dto) {
        return cardEnterpriseTemplateNewService.chooseAndSaveTemplateInfo(dto);
    }
}
